package com.projectdemo.springbootdemo1.controller;

import com.projectdemo.springbootdemo1.pojo.Department;
import com.projectdemo.springbootdemo1.pojo.Employee;
import com.projectdemo.springbootdemo1.service.DepartmentService;
import com.projectdemo.springbootdemo1.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 员工配置接口
 *
 * @date 2024/11/6
 * @author zhangzw
 **/
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;


    /**
     * 打开员工配置列表
     *
     * @param lang 语言
     * @param model 模型
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value = {"/{language}/employees","/{language}/employees.html"},method = RequestMethod.GET)
    public String employees(@PathVariable("language") String lang,Model model){
        model.addAttribute("lang",lang);
        Iterable<Employee> employees = employeeService.getAllEmployees();
        for (Employee employee : employees) {
            if (employee.getDepartmentId()!=null) {
                employee.setDepartment(departmentService.getDepartmentById(employee.getDepartmentId()));
            }
        }
        model.addAttribute("employeeList",employees);
        return "employees";
    }

    /**
     * 打开员工新增页面
     *
     * @param lang
     * @param model
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value = {"/{language}/employee"},method = RequestMethod.GET)
    public String employee(@PathVariable("language") String lang,Model model){
        model.addAttribute("lang",lang);
        Iterable<Department> departments = departmentService.getAllDepartments();
        model.addAttribute("departs",departments);
        return "empadd";
    }

    /**
     * 保存用户配置
     *
     * @param lang 语言
     * @param model 模型
     * @param employee 员工类
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value={"/{language}/employee","/{language}/employee.html"},method = RequestMethod.POST)
    public String employeeAdd(@PathVariable("language") String lang, Model model, Employee employee){
        model.addAttribute("lang",lang);
        employee.setDepartmentId(employee.getDepartment().getId());
        employeeService.saveEmployee(employee);
        return "redirect:employees";
    }

    /**
     * 进入员工修改页面
     *
     * @param lang 语言
     * @param eid 员工id
     * @param model 模型
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value={"/{language}/employee/{empid}"},method = RequestMethod.GET)
    public String toEmployeeEdit(@PathVariable("language") String lang,@PathVariable("empid") Integer eid, Model model){
        model.addAttribute("lang",lang);
        Employee employee = employeeService.getEmployeeById(eid);
        employee.setDepartment(departmentService.getDepartmentById(employee.getDepartmentId()));
        model.addAttribute("emp",employee);
        model.addAttribute("departs",departmentService.getAllDepartments());
        return "empedit";
    }

    /**
     * 更新员工配置值
     *
     * @param lang 语言
     * @param model 模型
     * @param employee 员工类
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value={"/{language}/employee","/{language}/employee.html"},method = RequestMethod.PUT)
    public String employeeUpdate(@PathVariable("language") String lang, Model model, Employee employee){
        model.addAttribute("lang",lang);
        employeeService.deleteEmployee(employee.getId());
        employee.setDepartmentId(employee.getDepartment().getId());
        employeeService.saveEmployee(employee);
        return "redirect:employees";
    }

    /**
     * 删除员工配置类
     *
     * @param lang 语言
     * @param eid 员工id
     * @param model 模型
     * @return java.lang.String data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @RequestMapping(value={"/{language}/empdel/{empid}"},method = RequestMethod.GET)
    public String employeeDel(@PathVariable("language") String lang,@PathVariable("empid") Integer eid, Model model){
        model.addAttribute("lang",lang);
        employeeService.deleteEmployee(eid);
        Iterable<Employee> employees = employeeService.getAllEmployees();
        for (Employee employee : employees) {
            employee.setDepartment(departmentService.getDepartmentById(employee.getDepartmentId()));
        }
        model.addAttribute("employeeList",employees);
        return "employees";
    }
}
